package com.example.backend.service.Impl.Prodcut;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.Mapper.BigTypeMapper;
import com.example.backend.Mapper.ProductMapper;
import com.example.backend.Mapper.SmallTypeMapper;
import com.example.backend.pojo.BigType;
import com.example.backend.pojo.Product;
import com.example.backend.pojo.SmallType;
import com.example.backend.service.BigType.BigTypeService;
import com.example.backend.service.Product.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private SmallTypeMapper smallTypeMapper;
    @Autowired
    private BigTypeMapper bigTypeMapper;

    @Override
    public List<Product> findSwiper() {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        List<Product> list = productMapper.selectList(queryWrapper.eq("isSwiper", true).orderByAsc("swiperSort"));
        return list;
    }

    @Override
    public JSONObject findHot(Integer page) {
        IPage<Product> Ipage = new Page<>(page, 8);
        QueryWrapper<Product> queryWrapper = new QueryWrapper();
        List<Product> lists = productMapper.selectPage(Ipage, queryWrapper.eq("isHot", true).orderByAsc("hotDateTime")).getRecords();
        JSONObject resp = new JSONObject();
        resp.put("hotProducts", lists);
        return resp;
    }

//    商品分类和全部商品查询
    public List<BigType> findCategories(){
        List<BigType> bigTypeLists = bigTypeMapper.selectList(null);
        for(BigType bigType : bigTypeLists){
            QueryWrapper<SmallType> smallQuery = new QueryWrapper<>();
            List<SmallType> smallTypeList = smallTypeMapper.selectList(smallQuery.eq("bigTypeId", bigType.getId()));
            bigType.setSmallTypeList(smallTypeList);
            for(SmallType smallType : smallTypeList){
                smallType.setProducts(productMapper.selectList(new QueryWrapper<Product>().eq("typeId", smallType.getId())));
            }
        }
        return bigTypeLists;
    }

}
